<template>
  <div class="app-container">
    <!-- 搜索 -->
    <el-form ref="searchForm" :model="searchForm" :inline="true" label-width="120px">
      <el-form-item label="文章标题" prop="title">
        <el-input v-model.trim="searchForm.title" />
      </el-form-item>
      <el-button type="primary" @click="onSubmit">搜索</el-button>
      <el-button @click="onCancel">取消</el-button>
    </el-form>
    <div class="form-action">
      <el-button type="success" size="mini" @click="handleAdd"> 新增 </el-button>
    </div>
    <!-- 表格 -->
    <el-table v-loading="listLoading" :data="list" element-loading-text="Loading" border fit highlight-current-row>
      <el-table-column align="center" label="ID" width="95">
        <template slot-scope="scope">
          {{ scope.$index + 1 + (pagination.pageNumber - 1) * pagination.pageSize }}
        </template>
      </el-table-column>
      <el-table-column label="文章标题">
        <template slot-scope="scope">
          {{ scope.row.title | ellipsis }}
        </template>
      </el-table-column>
      <el-table-column label="文章封面">
        <template slot-scope="scope">
          <img :src="scope.row.cover" alt="图片" width="auto" height="60">
        </template>
      </el-table-column>
      <el-table-column label="更新时间" width="200">
        <template slot-scope="scope">
          {{ scope.row.updatedAt }}
        </template>
      </el-table-column>

      <el-table-column align="center" label="操作" width="200">
        <template slot-scope="scope">
          <el-button type="primary" size="mini" @click="handleEdit(scope.row._id)">编辑</el-button>
          <el-button type="danger" size="mini" @click="handleDel(scope.row._id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <div class="block">
      <el-pagination
        background
        :current-page.sync="pagination.pageNumber"
        :page-size="pagination.pageSize"
        layout="total, prev, pager, next"
        :total="pagination.total"
        @current-change="handleCurrentChange"
      />
    </div>
  </div>
</template>

<script>
import { getNewList, delNews } from '@/api/news'
export default {
  name: 'News',
  data() {
    return {
      searchForm: {
        title: ''
      },
      pagination: {
        total: 0,
        pageSize: 5,
        pageNumber: 1
      },
      list: [],
      listLoading: false,
      refreshFlag: false
    }
  },
  created() {
    this.fetchData()
  },
  activated() {
    console.log(this.$route.meta.isFlushed)
    if (this.$route.meta.isFlushed) {
      this.fetchData()
    }
  },
  methods: {
    // 新增按钮
    handleAdd() {
      /*  this.$store.dispatch('tagsView/delView', this.$route).then(() => {
        this.$router.push('/news/editor')
      }) */
      this.$router.push('/news/add')
    },
    handleEdit(id) {
      /*     this.$store.dispatch('tagsView/delView', this.$route).then(() => {
        this.$router.push(`/news/editor?id=${id}`)
      }) */
      this.$router.push(`/news/edit?id=${id}`)
    },
    handleDel(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async() => {
          await delNews(id)
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          this.fetchData()
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    // 选择页码
    handleCurrentChange(val) {
      this.pagination.pageNumber = val
      this.fetchData(this.searchForm)
    },
    onSubmit() {
      this.fetchData(this.searchForm)
    },
    onCancel() {
      this.$refs.searchForm.resetFields()
    },
    async fetchData(searchForm = {}) {
      var { pageSize, pageNumber } = this.pagination
      this.listLoading = true
      try {
        const response = await getNewList({ pageSize, pageNumber, ...searchForm })
        this.list = response.data
        this.listLoading = false
        this.pagination = response.pagination
      } catch (error) {
        console.log('error: ', error)
        this.listLoading = false
      }
    }
  },
  beforeRouteLeave(to, from, next) {
    this.$route.meta.isFlushed = false
    next()
  }
}
</script>

<style lang="scss" scoped>
.form-action {
  margin-bottom: 20px;
  overflow: hidden;
  button {
    float: right;
  }
}
.block {
  margin-top: 20px;
  text-align: center;
}
</style>
